package fiszkas

import org.codehaus.groovy.grails.io.support.ClassPathResource

class ImportController {

    def grailsApplication
    def fiszkaImportService

    private static final gdocKeys = [
            karol: "0ArAobZQV1UeMdE5hZzgwMV9FUGd2c1YwVjJ0LTZqTFE",
            sabina: "0ArAobZQV1UeMdERKMTg4ZS14X2Zfb2RLQkdWeXdueEE"
    ]

    def index() {

    }

    def fromResources() {

        try {

            File file = new ClassPathResource("resources/test_import.tsv").getFile()

            def stats = fiszkaImportService.importFromFile(file)

            render(view: 'index', model: [msg: "Imported $stats"])

        } catch (e) {
            throw e
        }
    }

    def fromGoogle() {

        try {

            String ctx = request.getContextPath().replaceAll("/","").trim().toLowerCase()
            println ctx

            def key = gdocKeys[ctx];
            if(!key) {
                key = gdocKeys['karol']
                println "CONTEXT NOT MATCHING GDOC KEYS, using: $key"
            }

            def url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=${key}&exportFormat=tsv"

            HttpURLConnection conn = new URL(url).openConnection()
            conn.instanceFollowRedirects = true

            conn.addRequestProperty('accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
            conn.addRequestProperty('accept-charset', 'utf-8')
            conn.addRequestProperty('accept-language', 'pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4')
            conn.addRequestProperty('user-agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36')

            conn.addRequestProperty('cookie', 'HSID=AwDtBtLMWb8FoDbb9; SSID=A9wIARH3WJPgltG0g; APISID=YObKdjA6ebECMUEi/AQmKNaxH37SQcvco3; SAPISID=EbFiCOHV7jgP-QLs/AeGsfn7RxcUHunapX; PREF=ID=5b67ba48703c1670:U=140a50b98f5e63ff:FF=0:LD=pl:TM=1384878232:LM=1384880051:GM=1:S=-OEkA-RwxtHqg5HF; gvchan=586b6d353e67e4a433c1f58d24726af4%7C5544249e918796008d195fbc0ff76039=EgAAAKgUlIgk2pNYySSy5pOIPapktx3Om%2BA1kNyD7fdMgL64; NID=67=aDHDoO7t4fK7JRvfoW9HUQs91MoVO3sdUIOL_HqPvx9Y0jNUPQnholpZW82_252dmBbpVNyk4YcxTtzf3HOAuxUJYVR4vZcfZUoH02_etHwAzCx437etlD6AYSQcTP9V-0RSSn4hVSGxaLYrt3WcvbFAnKEDO748hWmDTN9Rml9SAtxm5yMrHCpBzccla8wAum1VyMTpRzcf1g; SID=DQAAANsAAAATEobxcOMvmUvXctgi7QplDQhW5iqikHOWZz6rcArTj4OF1H6vYFqWQwMcNHMBbJzWwOyBPLzuPcJ5vX5s_PYHIpzbolrxUQ3f9Bqx2hWqKZQgPdzyDkrQi8vYy9duQ1qHWBtLUtsyt_-BQtvx2p18Kbrp93kG41GvWfjqHXavQ-fLGydoGfaHmXiSYkxvoTdUjo_UPGi9z6vlGdI0_e6uIgneiSaPbHj2QOS-PQyXaV8JZUfJ_Wi5QYvtiyQ1hFPxgvkEg57KbJhrVCjbY4LeqcHDLla06ygYBEvRkkXuhw')

            def txt = conn.content.text

//            def out = new ByteArrayOutputStream()
//            out << conn.inputStream
//            out.close()

//            def txt = conn.inputStream.getText('UTF-8')

//            FileOutputStream file = new FileOutputStream("tmp.tsv")
//            def out = new BufferedOutputStream(file)
//            out << conn.inputStream
//            out.close()
//
//            println new File("tmp.tsv").getText('UTF-8')

            def stats = fiszkaImportService.importFromTxt(txt)

            render(view: 'index', model: [msg: "Imported $stats"])

        } catch (e) {
            throw e
        }

    }
}
